package com.aqie.appletserver.controller;

import com.aqie.appletserver.bean.VoiceList;
import com.aqie.appletserver.bean.request.VoiceRequest;
import com.aqie.appletserver.bean.response.VoiceResponse;
import com.aqie.appletserver.common.response.ResponseModel;
import com.aqie.appletserver.context.TokenContext;
import com.aqie.appletserver.service.IBaiduApiService;
import com.aqie.appletserver.service.IUploadService;
import com.aqie.appletserver.service.IVoiceListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("upload")
@Slf4j
public class UploadController {
    @Value("${file.serverUrl}")
    private String SERVER_URL;

    @Autowired
    private IBaiduApiService baiduApiService;

    @Autowired
    private IVoiceListService voiceListService;

    @Autowired
    private IUploadService uploadService;


    // 文件上传
    @RequestMapping("/file_upload")
    public ResponseModel<Map<String, String>> saveFile(HttpServletRequest request) throws Exception {
        Map<String, String> resMap = uploadService.uploadFile(request);
        return ResponseModel.success(resMap);
    }



    // 音频上传
    @RequestMapping(value = "/voice_upload", produces = "application/json")
    @ResponseBody
    public ResponseModel<String> voice_upload(HttpServletRequest request,
                                          @RequestParam("file") MultipartFile files,String openid,String deviceid) throws IOException {
        Map<String, String> resMap = uploadService.uploadAudio(request, files);
        if(resMap != null){
            VoiceRequest voiceRequest = new VoiceRequest();
            voiceRequest.setFILENAME(resMap.get("localSavePath"));
            VoiceResponse response  = baiduApiService.speechToText(voiceRequest, TokenContext.getToken());

            // 保存用户openid 地址 设备信息 到数据库
            VoiceList voiceList = new VoiceList();
            voiceList.setDeviceid(deviceid);
            voiceList.setCreateTime((int) (System.currentTimeMillis() / 1000));
            voiceList.setOpenid(openid);
            voiceList.setVoice_url(resMap.get("databaseSavePath"));
            voiceList.setContent(response.getResult().get(0));
            voiceListService.save(voiceList);
            return ResponseModel.success(SERVER_URL + resMap.get("databaseSavePath"));
        }
        return ResponseModel.error("上传失败");

    }

    public static void main(String[] args) {
        log.info("{}","oACpq5Bq2jKMeYjTfEpQCAILrO-U".length());
    }
}
